package drone.basis.util ;

import java.io.Serializable ;
import java.util.Objects ;

import io.swagger.annotations.ApiModel ;
import io.swagger.annotations.ApiModelProperty ;

@ApiModel ( "分页对象" )
public class PageUtil implements Serializable
{

	private static final long serialVersionUID = 1L ;

	@ApiModelProperty ( value = "当前页码" , dataType = "Integer" , example = "1" )
	private Integer pageIndex = 1 ;

	@ApiModelProperty ( value = "页面记录数" , dataType = "Integer" , example = "20" )
	private Integer pageSize = 20 ;

	@ApiModelProperty ( value = "总记录数" , dataType = "Integer" , example = "0" )
	private Integer pageRecord = 0 ;

	@ApiModelProperty ( value = "总页数" , dataType = "Integer" , example = "1" )
	private Integer pageGross = 1 ;

	@ApiModelProperty ( value = "上页页码" , dataType = "Integer" , example = "1" )
	private Integer pagePrevious = 1 ;

	@ApiModelProperty ( value = "下页页码" , dataType = "Integer" , example = "1" )
	private Integer pageNext = 1 ;

	@ApiModelProperty ( value = "隐藏页面数" , dataType = "Integer" , example = "4" )
	private Integer pageHide = 4 ;

	@ApiModelProperty ( value = "显示页头" , dataType = "Integer" , example = "1" )
	private Integer pageTop = 1 ;

	@ApiModelProperty ( value = "显示页尾" , dataType = "Integer" , example = "1" )
	private Integer pageEnd = 1 ;

	public PageUtil ( )
	{

	}

	public PageUtil (
			Integer pageIndex ,
			Integer pageSize
	)
	{
		if ( ! Objects.isNull ( pageIndex ) )
		{
			this.pageIndex = pageIndex ;
		}
		if ( ! Objects.isNull ( pageSize ) )
		{
			this.pageSize = pageSize ;
		}
	}

	public PageUtil (
			String pageIndex ,
			String pageSize
	)
	{
		if ( ! StringUtil.isEmpty ( pageIndex ) && StringUtil.isDigit ( pageIndex ) )
		{
			this.pageIndex = new Integer ( pageIndex ) ;
		}
		if ( ! StringUtil.isEmpty ( pageSize ) && StringUtil.isDigit ( pageSize ) )
		{
			this.pageSize = new Integer ( pageSize ) ;
		}
	}

	public PageUtil (
			Object pageIndex ,
			Object pageSize
	)
	{
		if ( ! Objects.isNull ( pageIndex ) && StringUtil.isDigit ( pageIndex.toString ( ) ) )
		{
			this.pageIndex = new Integer ( pageIndex.toString ( ) ) ;
		}
		if ( ! Objects.isNull ( pageSize ) && StringUtil.isDigit ( pageSize.toString ( ) ) )
		{
			this.pageSize = new Integer ( pageSize.toString ( ) ) ;
		}
	}

	public void allocation ( )
	{
		setPageGross ( ( ( getPageRecord ( ) == 0 ) ? 1 : ( getPageRecord ( ) % getPageSize ( ) == 0 ) ? ( getPageRecord ( ) / getPageSize ( ) ) : ( getPageRecord ( ) / getPageSize ( ) + 1 ) ) ) ;
		setPageIndex ( ( getPageIndex ( ) > getPageGross ( ) ) ? 1 : getPageIndex ( ) ) ;
		setPageNext ( ( ( getPageIndex ( ) + 1 ) < getPageGross ( ) ) ? ( getPageIndex ( ) + 1 ) : getPageGross ( ) ) ;
		setPagePrevious ( ( ( getPageIndex ( ) - 1 ) > 1 ) ? ( getPageIndex ( ) - 1 ) : 1 ) ;
		setPageTop ( getPageIndex ( ) - getPageHide ( ) > 0 ? getPageIndex ( ) - getPageHide ( ) : 1 ) ;
		setPageEnd ( getPageIndex ( ) + getPageHide ( ) < getPageGross ( ) ? getPageIndex ( ) + getPageHide ( ) : getPageGross ( ) ) ;
	}

	public Integer getPageIndex ( )
	{
		return pageIndex ;
	}

	public void setPageIndex (
			Integer pageIndex
	)
	{
		this.pageIndex = pageIndex ;
	}

	public Integer getPageSize ( )
	{
		return pageSize ;
	}

	public void setPageSize (
			Integer pageSize
	)
	{
		this.pageSize = pageSize ;
	}

	public Integer getPageRecord ( )
	{
		return pageRecord ;
	}

	public void setPageRecord (
			Integer pageRecord
	)
	{
		this.pageRecord = pageRecord ;
	}

	public Integer getPageGross ( )
	{
		return pageGross ;
	}

	public void setPageGross (
			Integer pageGross
	)
	{
		this.pageGross = pageGross ;
	}

	public Integer getPagePrevious ( )
	{
		return pagePrevious ;
	}

	public void setPagePrevious (
			Integer pagePrevious
	)
	{
		this.pagePrevious = pagePrevious ;
	}

	public Integer getPageNext ( )
	{
		return pageNext ;
	}

	public void setPageNext (
			Integer pageNext
	)
	{
		this.pageNext = pageNext ;
	}

	public Integer getPageHide ( )
	{
		return pageHide ;
	}

	public void setPageHide (
			Integer pageHide
	)
	{
		this.pageHide = pageHide ;
	}

	public Integer getPageTop ( )
	{
		return pageTop ;
	}

	public void setPageTop (
			Integer pageTop
	)
	{
		this.pageTop = pageTop ;
	}

	public Integer getPageEnd ( )
	{
		return pageEnd ;
	}

	public void setPageEnd (
			Integer pageEnd
	)
	{
		this.pageEnd = pageEnd ;
	}

	public String toString ( )
	{
		StringBuffer sb = new StringBuffer ( ) ;
		sb.append ( "总记录数[ pageRecord ] : " + getPageRecord ( ) + System.lineSeparator ( ) ) ;
		sb.append ( "总页数[ pageGross ] : " + getPageGross ( ) + System.lineSeparator ( ) ) ;
		sb.append ( "当前页[ pageIndex ] : " + getPageIndex ( ) + System.lineSeparator ( ) ) ;
		sb.append ( "每页记录数[ pageSize ] : " + getPageSize ( ) + System.lineSeparator ( ) ) ;
		sb.append ( "上一页[ pagePrevious ] : " + getPagePrevious ( ) + System.lineSeparator ( ) ) ;
		sb.append ( "下一页[ pageNext ] : " + getPageNext ( ) + System.lineSeparator ( ) ) ;
		return sb.toString ( ) ;
	}

}
